//
//  JYTPostIdearViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 17/1/17.
//  Copyright © 2017年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTPostIdearViewController.h"
#import "JYTPlaceholderTextView.h"
#import "JYTLoginHandler.h"

@interface JYTPostIdearViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *lineTop;
@property (nonatomic, strong) UIView *lineBotom;
@property (nonatomic, strong) UIView *phoneNumView;
@property (nonatomic, strong) UIView *lineBottomPhone;
@property (nonatomic, strong) UIView *lineTopPhone;
@property (nonatomic, strong) UILabel *phoneNumLabel;

@property (nonatomic, strong) UIView *mailView;
@property (nonatomic, strong) UILabel *mailLabel;
@property (nonatomic, strong) JYTPlaceholderTextView *textView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITextField *textFieldMail;

@end

@implementation JYTPostIdearViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"意见反馈",
                          @"rhtBtnBgImg":@"no",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];
    
    [self addSubView];
}

- (void)addSubView
{

    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = kColor(240, 240, 240);
    self.topView = topView;
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(74);
        make.height.equalTo(@(150));
        
    }];


    UIView *lineTop = [[UIView alloc]init];
    lineTop.backgroundColor = kColor(192, 192, 192);
    self.lineTop = lineTop;
    [self.topView addSubview:lineTop];
    [lineTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView.mas_left);
        make.right.equalTo(self.topView.mas_right);
        make.top.equalTo(self.topView.mas_top);
        make.height.equalTo(@(0.5));
        
    }];
    
    UIView *lineBotom = [[UIView alloc]init];
    self.lineBotom = lineBotom;
    lineBotom.backgroundColor = kColor(192, 192, 192);
    [self.topView addSubview:lineBotom];
    [lineBotom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView.mas_left);
        make.right.equalTo(self.topView.mas_right);
        make.bottom.equalTo(self.topView.mas_bottom);
        make.height.equalTo(@(0.5));
        
    }];

    JYTPlaceholderTextView *textView = [[JYTPlaceholderTextView alloc] init];
    textView.backgroundColor = [UIColor clearColor];
    textView.placeholder = @"亲，我们非常重视您给我们提出的宝贵建议，帮助我们不断完善产品，谢谢!";
    textView.placeholderColor = [UIColor colorWithRed:144 / 255.0 green:144 / 255.0 blue:144 / 255.0 alpha:1];
    textView.tColor = [UIColor colorWithRed:96 / 255.0 green:96 / 255.0 blue:96 / 255.0 alpha:1];
    textView.font = [UIFont systemFontOfSize:14];
    self.textView = textView;
    [self.topView addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.topView.mas_left).offset(20);
        make.right.equalTo(self.topView.mas_right).offset(-12);
        make.top.equalTo(self.lineTop.mas_bottom).offset(10);
        make.bottom.equalTo(self.lineBotom.mas_top).offset(-10);
        
    }];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *normal =[self createImageWithColor:[UIColor colorWithRed:131/255.0f green:182/255.0f blue:248/255.0f alpha:1]];
    UIImage *highlighted =[self createImageWithColor:[UIColor colorWithRed:131/255.0f green:182/255.0f blue:248/255.0f alpha:1]];
    [btn setBackgroundImage:normal forState: UIControlStateNormal];
    [btn setBackgroundImage:highlighted forState: UIControlStateHighlighted];
    [btn setTitle:@"确认提交" forState:UIControlStateNormal];
    [btn setTitleColor:kColor(240, 240, 240) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [btn addTarget:self action:@selector(commitIdear) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.equalTo(@(49));
    }];

    
    
}

- (void)commitIdear
{
    NSString *content =  self.textView.text;
    if (!content || [content isEqualToString:@" "]|| [content isEqualToString:@""]) {
        [self showMessage:@"意见不能为空"];
        return;
    }
    
    User * u = [[JYTUserManager shared] getUser];
    NSString *uName =u.userName;
   
    [[JYTLoginHandler shared] postContent:content andUserName:uName completionHandler:^(BOOL succ) {
        
        if (succ) {
            [self showMessage:@"提交成功"];
            [self goBack];
        } else {
             [self showMessage:@"提交失败，请重新提交！"];
        }
        
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (UIImage *)createImageWithColor:(UIColor *)color

{
    
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return theImage;
    
}

- (void)showMessage:(NSString *)message
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = message;
    
    //    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    //初始化段落，设置段落风格
    
    NSMutableParagraphStyle *paragraphstyle=[[NSMutableParagraphStyle alloc]init];
    paragraphstyle.lineBreakMode=NSLineBreakByCharWrapping;
    
    //设置label的字体和段落风格
    
    NSDictionary *dic=@{NSFontAttributeName:label.font,NSParagraphStyleAttributeName:paragraphstyle.copy};
    
    //计算label的真正大小,其中宽度和高度是由段落字数的多少来确定的，返回实际label的大小
    
    CGRect rect=[label.text boundingRectWithSize:CGSizeMake(290, 9000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGSize LabelSize = CGSizeMake(rect.size.width, rect.size.height);
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    [showview addSubview:label];
    showview.frame = CGRectMake((CURRENT_SCREEN_WIDTH - LabelSize.width - 20)/2, CURRENT_SCREEN_HEIGHT - 100, LabelSize.width+20, LabelSize.height+10);
    [UIView animateWithDuration:2 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
